var aa = $(".tt_type");
var t_type = new Array();
var t_dao = new Array();
var t_fen = new Array();
var type_name = new Array();
var lity = function () {
    var i = 0;
    aa.each(
        function () {
            var a = $(this);
            t_type[i] = a.attr("id");
            t_dao[i] = a.find(".dao").val();
            t_fen[i] = a.find(".fen").val();
            type_name[i] = a.find(".type_name").val();
            i++;
        }
    );
}

$("#a_type").click(
    function () {
        if ($("#a_type").val() == 1) {
            $("#z_type").show();
        } else {
            $("#z_type").hide();
            $("#saveBtn").click(
                function () {
                    sdon();
                }
            );
        }
        ;
    }
);

var sdon = function () {
    $.ajax({
        type:"GET",
        url:"/paper/sdonAddPaper.do",
        data:{
            "paper_name":$("#paper_name").val(),
            "ptype.ptype_id":$("#ptype_id").val(),
            "aClass.class_id":$("#class_id").val(),
            "p_key":$("#pwd").val(),
        },
        dataType:"json",
        success:function (data) {
            alert("手动添加成功");
        }

    })
}
$("#lib").click(
    function () {
        $.ajax({
            type: "GET",
            dataType: "json",
            url: "/paper/showPoint.do",
            data: {
                id: $("#lib").val(),
            },
            success: function (msg) {
                $("#point").html("");
                for (var a in msg)
                    $("#point").prepend("<option id='ad' value='"+msg[a].point_id+"'>" + msg[a].point_name + "</option>");
            }
        });
    }

);
var show = new Array();
show[0] = 0;
show[1] = 0;
show[2] = 0;
var daoz = new Array();
$("#addPoint").click(
    function () {
        var option = $("#point option:selected");
        var options =$("#cpoint option");
        if (options.length==0){
            checkPoint(option);
        }else{
            for (var i = 0;i<options.length;i++){
                if(option.text()==$(options[i]).text()){
                    alert("sds");
                    break;
                }else{
                    checkPoint(option);
                    break;
                }
            }
        }
    }
);
var checkPoint = function (option) {
    $("#cpoint").prepend("<option value='"+option.val()+"'>" + option.text() + "</option>");
    lity();
    $.ajax({
        type:"GET",
        url:"/problem/getTypeAndPointCount.do",
        data:{
            type:t_type,
            point:option.val(),
        },
        dataType:"json",
        success:function (data) {
            var c = $(".daos");
            var ds = new Array();
            var i = 0;
            c.each(
                function () {
                    var a = $(this);
                    ds[i] = a;
                    i++;
                }
            );
            for (var i in data){
                show[i]+=data[i];
                ds[i].html("<span>"+"最多为:"+show[i]+"道"+"<input type='hidden' class='hidden-1' value='show[i]'>"+"</span>");
            }
        },
        error:function () {
            alert("sd");
        }
    });
}
$("#delePoint").click(
    function () {
        var option = $("#cpoint option:selected");
        option.remove();
    }
);
$("#clarePoint").click(
    function () {
        $("#cpoint").empty();
    }
);
$("#c_pwd").click(
    function () {
        $("#pwd").val(Math.floor(Math.random() * 1000000000));
    }
);


var addPaPer = function () {
    $.ajax({
        type: "GET",
        dataType: "text",
        url: "/paper/addPaper.do",
        data: {
            ids:t_type,
            daos:t_dao,
            fens:t_fen,
            t_name:type_name,
            options:option,
            paper_name:$("#paper_name").val(),
            ptype_id:$("#ptype_id").val(),
            class_id:$("#class_id").val(),
            pwd:$("#pwd").val(),
            full:$("#full").val(),
            pass:$("#pass").val(),
            dura:$("#dura").val()
        },
        success: function (data) {
            alert(data);
            yes();
        }
    });
}

function yes(){
    var index = parent.layer.getFrameIndex(window.name);
    parent.getUserPageList();
    parent.layer.close(index);
    parent.layer.msg("添加成功",{icon: 1});
}
$("#saveBtn").on("click", function () {
    points();
    lity();
    addPaPer();

});

$(".fen,.dao").change(
    function () {
        lity();
        var a = 0;
        var b = 0;
        var full = a * b;
        for (var i = 0; i < t_type.length; i++) {
            a = t_dao[i];
            b = t_fen[i];
            full += a * b;
        }
        $("#full").val(full);
    }
);
var option = new Array();
var points = function () {
    var options =$("#cpoint option");
    for (var i = 0;i<options.length;i++){
        option[i] = ($(options[i]).attr("value"));
    }
}
function changeDao(id,dao,sd) {
    $.ajax({
        type:"GET",
        url:"/problem/getTypeProblemCount.do",
        data: {
            "type_id":id,
        },
        dataType:"json",
        success:function (data) {
            if (data<dao){
                sd.html("<span>"+"最多为:"+data+"道"+"<input type='hidden' class='hidden-1' value='data'>"+"</span>");
            }else {
                sd.html("");
            }
        }
    })
}